using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NozzleBtn : MonoBehaviour
{
    private Button _button;
    private Image _selectImg;
    private Image _nozzleImg;
    private Transform _lock;

    private int _index;
    private Action<int> _action;
    private bool _unLock;

    public void Init(bool unLock)
    {
        _unLock = unLock;
        _button = GetComponent<Button>();
        _button.onClick.AddListener(OnClick);
        var go = transform.Find("SelectImg");
        _selectImg = transform.Find("SelectImg").GetComponent<Image>();
        _nozzleImg = transform.Find("NozzleImg").GetComponent<Image>();
        _lock = transform.Find("Lock");
        _lock.gameObject.SetActive(!unLock);
    }

    public void Set(int index, Action<int> action)
    {
        _index = index;
        _action = action;
        string path = $"{Constants.PathNozzle}_{index}";
        var go = GameManager.Instance.LoadSprite($"{Constants.PathNozzle}_{index}");
        _nozzleImg.sprite = GameManager.Instance.LoadSprite($"{Constants.PathNozzle}_{index}");
        _nozzleImg.SetNativeSize();
    }

    private void OnClick()
    {
        if (!_unLock)
        {
            AdManager.ShowVideo(() =>
            {
                this._unLock = true;
                this._lock.gameObject.SetActive(false);
                _action?.Invoke(_index);
            });
            return;
        }

        _action?.Invoke(_index);
    }

    public void SetSelect(int index) => _selectImg.gameObject.SetActive(_index == index);
}